package com.ms.oschina.ui.activity;

import java.util.ArrayList;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.AxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IRadarDataSet;
import com.ms.oschina.R;
import com.ms.oschina.base.BaseActivity;
import com.ms.oschina.view.RadarMarkerView;

public class SkillsRadarActivity extends BaseActivity {
	private RadarChart mChart;

	private String[] skills = new String[] { "安卓", "JAVA", "IOS", "EE", "C++" };

	private Typeface tf;

	private void findViews() {
		mChart = (RadarChart) findViewById(R.id.pc_skilladar);
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_skillradar;
	}

	@Override
	protected void initView() {
		findViews();
	}

	@Override
	protected void initData() {
		tf = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");

		mChart.setBackgroundColor(Color.rgb(60, 65, 82));

		mChart.setDescription("");

		mChart.setWebLineWidth(1f);
		mChart.setWebColor(Color.LTGRAY);
		mChart.setWebLineWidthInner(1f);
		mChart.setWebColorInner(Color.LTGRAY);
		mChart.setWebAlpha(100);

		// create a custom MarkerView (extend MarkerView) and specify the layout
		// to use for it
		MarkerView mv = new RadarMarkerView(this, R.layout.radar_markerview);

		// set the marker to the chart
		mChart.setMarkerView(mv);

		setData();

		mChart.animateXY(1400, 1400, Easing.EasingOption.EaseInOutQuad,
				Easing.EasingOption.EaseInOutQuad);

		XAxis xAxis = mChart.getXAxis();
		xAxis.setTextSize(9f);
		xAxis.setTypeface(tf);
		xAxis.setYOffset(0f);
		xAxis.setXOffset(0f);
		xAxis.setValueFormatter(new AxisValueFormatter() {

			@Override
			public String getFormattedValue(float value, AxisBase axis) {
				return skills[(int) value % skills.length];
			}

			@Override
			public int getDecimalDigits() {
				return 0;
			}
		});
		xAxis.setTextColor(Color.WHITE);

		YAxis yAxis = mChart.getYAxis();
		yAxis.setTypeface(tf);
		yAxis.setLabelCount(5, false);
		yAxis.setTextSize(9f);
		yAxis.setAxisMinValue(0f);
		yAxis.setAxisMaxValue(80f);
		yAxis.setDrawLabels(false);

		Legend l = mChart.getLegend();
		l.setTypeface(tf);
		l.setPosition(LegendPosition.ABOVE_CHART_CENTER);
		l.setXEntrySpace(7f);
		l.setYEntrySpace(5f);
		l.setTextColor(Color.WHITE);
	}

	public void setData() {

		float mult = 80;
		float min = 20;
		int cnt = 5;

		ArrayList<RadarEntry> entries1 = new ArrayList<RadarEntry>();
		ArrayList<RadarEntry> entries2 = new ArrayList<RadarEntry>();

		// 当被添加到条目数组中的条目的顺序etermines他们围绕的中心位置图表。
		for (int i = 0; i < cnt; i++) {
			float val1 = (float) (Math.random() * mult) + min;
			entries1.add(new RadarEntry(val1));

			float val2 = (float) (Math.random() * mult) + min;
			entries2.add(new RadarEntry(val2));
		}

		RadarDataSet set1 = new RadarDataSet(entries1, "学习");
		set1.setColor(Color.rgb(103, 110, 129));
		set1.setFillColor(Color.rgb(103, 110, 129));
		set1.setDrawFilled(true);
		set1.setFillAlpha(180);
		set1.setLineWidth(2f);
		set1.setDrawHighlightCircleEnabled(true);
		set1.setDrawHighlightIndicators(false);

		RadarDataSet set2 = new RadarDataSet(entries2, "复习");
		set2.setColor(Color.rgb(121, 162, 175));
		set2.setFillColor(Color.rgb(121, 162, 175));
		set2.setDrawFilled(true);
		set2.setFillAlpha(180);
		set2.setLineWidth(2f);
		set2.setDrawHighlightCircleEnabled(true);
		set2.setDrawHighlightIndicators(false);

		ArrayList<IRadarDataSet> sets = new ArrayList<IRadarDataSet>();
		sets.add(set1);
		sets.add(set2);

		RadarData data = new RadarData(sets);
		data.setValueTypeface(tf);
		data.setValueTextSize(8f);
		data.setDrawValues(false);
		data.setValueTextColor(Color.WHITE);

		mChart.setData(data);
		mChart.invalidate();
	}

	@Override
	protected CharSequence getBackActionBarTitle() {
		return "技能雷达";
	}

}
